﻿///----------------------------------------------------------------
/// Copyright (C) 2008-2010 yyssoft
/// 版权所有。
///
/// 文件名：TB_DDJDAL.cs
/// 文件功能描述：TB_DDJ数据访问类
///
///
/// 创建标识：yyssoft 2013/11/20
///
/// 修改标识：
/// 修改描述：
///
/// 修改标识：
/// 修改描述：
///---------------------------------------------------------------
using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using DQYT.HTDZ.Model;
using System.Data.OracleClient;
using Maticsoft.DBUtility;

namespace DQYT.HTDZ.DAL
{
    /// <summary>
    /// TB_DDJ的数据访问类
    /// </summary>
    public class TB_DDJDAL 
    {

        #region  构造函数

        public TB_DDJDAL()
        { }

        #endregion  构造函数

        #region  成员方法

        /// <summary>
        /// 获取最大ID
        /// </summary>
        /// <returns>最大ID</returns>
        public DataSet getMaxID()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT MAX(ID) as maxid FROM TB_DDJ ");
            return DbHelperOra.Query(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回主标识ID,失败返回"0"</returns>
        public bool AddBySql(TB_DDJInfo model)
        {
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append(@" INSERT INTO TB_DDJ
					(ID,DDJ,DX) 
					VALUES(:ID,:DDJ,:DX)");

            OracleParameter[] parameters = {
					new OracleParameter(":"+TB_DDJInfo.ID_FIELD, OracleType.Number,4),
					new OracleParameter(":"+TB_DDJInfo.DDJ_FIELD, OracleType.Number,4),
					new OracleParameter(":"+TB_DDJInfo.DX_FIELD, OracleType.VarChar,50)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.DDJ == int.MinValue ? DBNull.Value : (object)model.DDJ;
            parameters[2].Value = model.DX;

            int rows = DbHelperOra.ExecuteSql(sbSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回"1",失败返回"0"</returns>
        public bool UpdateBySql(TB_DDJInfo model)
        {
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append(@" UPDATE TB_DDJ
					SET DDJ = :DDJ,DX = :DX 
					WHERE ID=:ID ");

            OracleParameter[] parameters = {
					new OracleParameter(":"+TB_DDJInfo.ID_FIELD, OracleType.Number,4),
					new OracleParameter(":"+TB_DDJInfo.DDJ_FIELD, OracleType.Number,4),
					new OracleParameter(":"+TB_DDJInfo.DX_FIELD, OracleType.VarChar,50)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.DDJ == int.MinValue ? DBNull.Value : (object)model.DDJ;
            parameters[2].Value = model.DX;

            int rows = DbHelperOra.ExecuteSql(sbSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回"1",失败返回"0"</returns>
        public bool DeleteBySql(int ID)
        {
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append(@" DELETE TB_DDJ 
					WHERE ID=:ID ");

            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4)};
            parameters[0].Value = ID;

            int rows = DbHelperOra.ExecuteSql(sbSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TB_DDJInfo GetModelBySql(int ID)
        {
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append(@"SELECT ID,DDJ,DX 
					FROM TB_DDJ
					WHERE ID=:ID ");

            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4)};
            parameters[0].Value = ID;

            TB_DDJInfo model = new TB_DDJInfo();

            DataSet ds = DbHelperOra.Query(sbSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["DDJ"] != null && ds.Tables[0].Rows[0]["DDJ"].ToString() != "")
                {
                    model.DDJ =  Convert.ToDecimal(ds.Tables[0].Rows[0]["DDJ"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DX"] != null && ds.Tables[0].Rows[0]["DX"].ToString() != "")
                {
                    model.DX = ds.Tables[0].Rows[0]["DX"].ToString();
                }
            }
            
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT *
					FROM TB_DDJ ");
            strSql.Append(" where 1 = 1 ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" and " + strWhere);
            }
            return DbHelperOra.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM TB_DDJ ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperOra.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.JH desc");
            }
            strSql.Append(")AS Row, T.*  from TB_DDJ T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperOra.Query(strSql.ToString());
        }

        #endregion  成员方法

    }
}

